<?php

namespace ZfcUser\Mapper;

use ZfcBase\Mapper\AbstractDbMapper;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class User extends AbstractDbMapper implements UserInterface, ServiceLocatorAwareInterface
{
    protected $services;
    protected $tableName  = 'user';

    public function findByEmail($email)
    {
        $select = $this->getSelect()
                       ->where(array('email' => $email));

        $entity = $this->select($select)->current();
        $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
        return $entity;
    }

    public function findByUsername($username)
    {
        $select = $this->getSelect()
                       ->where(array('username' => $username));

        $entity = $this->select($select)->current();
        $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
        return $entity;
    }

    public function findById($id)
    {
        $select = $this->getSelect()
                       ->where(array('user_id' => $id));

        $entity = $this->select($select)->current();
        $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
        return $entity;
    }

    public function getTableName(){
        return $this->tableName;
    }
    
    public function setTableName($tableName){
        $this->tableName=$tableName;
    }    
    
    public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
    {
        $result = parent::insert($entity, $tableName, $hydrator);
        $entity->setId($result->getGeneratedValue());
        
        // TODO : find a better way to insert the new user in user_role_linker
        $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        $connexion = $em->getConnection();
        $sql = "insert into user_role_linker(user_id, role_id) values (:id, 'user')";
        $connexion->executeQuery($sql, array('id' => $entity->getId()));
        
        // Insert the new user in table "utilisateur"
        $boutique = new \Application\Model\Entity\Boutique(null, 'Bienvenue sur ma boutique !', 0, 'Decrivez votre boutique en quelques lignes ...');
        $utilisateur = new \Application\Model\Entity\Utilisateur($entity->getId(), 0, '', '', $entity->getEmail(), '', '', $boutique);
        $em->persist($utilisateur);
        $em->flush();
        
        return $result;
    }

    public function update($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null)
    {
        if (!$where) {
            $where = 'user_id = ' . $entity->getId();
        }

        return parent::update($entity, $where, $tableName, $hydrator);
    }

    /**********************************************
    * ServiceLocatorAwareInterface implementation *
    **********************************************/
    public function getServiceLocator()
    {
        return $this->services; 
    }

    public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
    {
        $this->services = $serviceLocator;    
    }
}
